package com.payment.ktb.activity.main4;

import android.app.Dialog;
import android.content.Context;
import android.media.Image;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.flyco.animation.BaseAnimatorSet;
import com.flyco.animation.FlipEnter.FlipVerticalSwingEnter;
import com.flyco.dialog.widget.base.TopBaseDialog;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.payment.ktb.Interface.AlertInterface;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.adapter.InviteRecordAdapter;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.model.InviteRecordEntity;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by admin on 2017/9/4.
 */

public class InviteRecordActivity extends BaseActivity {


    @BindView(R.id.tv_inviterecord_notes)
    TextView tv_inviterecord_notes;

    @BindView(R.id.tv_inviterecord_hascharge)
    TextView tv_inviterecord_hascharge;

    @BindView(R.id.btn_inviterecord_exchange)
    Button btn_inviterecord_exchange;
    @BindView(R.id.lv_inviterecord)
    ListView lv_inviterecord;
    @BindView(R.id.rl_inviterecord_nochargedhead)
    RelativeLayout rl_inviterecord_nochargedhead;


    List<InviteRecordEntity> listDates;
    InviteRecordAdapter inviteRecordAdapter;
    private boolean noChargedFlag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inviterecord);
        initTitle("邀请记录");
        ButterKnife.bind(this);
        LinearLayout ll_title_back = (LinearLayout)findViewById(R.id.ll_title_back);
        ll_title_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (noChargedFlag) {
                    finish();
                }else {
                    noChargedFlag =true;
                    initDate();
                }
            }
        });

        initDate();
    }

    private void initDate() {

        rl_inviterecord_nochargedhead.setVisibility(View.VISIBLE);

        if (getIntent().getIntExtra("isCharge",0) == 0) {
            btn_inviterecord_exchange.setEnabled(false);
            tv_inviterecord_notes.setText("再邀请" + getIntent().getIntExtra("residue",0) + "位好友可兑换会员资格");
        }else {
            btn_inviterecord_exchange.setEnabled(true);
            tv_inviterecord_notes.setText("已成功邀请" + getIntent().getIntExtra("chargeUser",0) + "位好友，可兑换会员资格");
        }



        //date
        listDates = new Gson().fromJson(getIntent().getStringExtra("records"), new TypeToken<List<InviteRecordEntity>>() {}.getType());
        //adapter
        inviteRecordAdapter = new InviteRecordAdapter(mContext,listDates);
        //bind
        lv_inviterecord.setAdapter(inviteRecordAdapter);

    }


    @OnClick({R.id.btn_inviterecord_exchange,R.id.tv_inviterecord_hascharge})
    public void clickEvent(View view){

        switch (view.getId()){

            //兑换
            case R.id.btn_inviterecord_exchange:
                AlertDialogUtils.showAlertDialog(mContext, "确定用" + getIntent().getIntExtra("chargeUser",0) + "个邀请名额兑换" + getIntent().getIntExtra("chargeDay",0) + "天会员权益？", "取消", "确定", new AlertInterface() {
                    @Override
                    public void confirm(Dialog alertDialog) {
                        alertDialog.dismiss();
                        exchangeVip();
                    }
                });
                break;

            //查看已经兑换的名单
            case R.id.tv_inviterecord_hascharge:

                getChargedList();

                break;

        }
    }

    //兑换会员
    private void exchangeVip() {


        progressDialogUtils.showProgress();

        //构造传输参数
        Map<String, String> map = new HashMap<String,String>();
        map.put("chargeUser",getIntent().getIntExtra("chargeUser",0) + "");
        map.put("chargeDay",getIntent().getIntExtra("chargeDay",0) + "");

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.UserInviteExchange, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                progressDialogUtils.dismissProgress();
                    ToastUtils.showToast("兑换成功！");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                       finish();
                    }
                },1000);
            }

            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });

    }


    public void getChargedList() {


        progressDialogUtils.showProgress();

        //构造传输参数
        Map<String, String> map = new HashMap<String,String>();
        map.put("isCharge","1");

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.UserInviteList, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                progressDialogUtils.dismissProgress();
                try {
                    JSONObject jsonObject = new JSONObject(response);

                    btn_inviterecord_exchange.setEnabled(false);

                    //date
                    listDates = new Gson().fromJson(jsonObject.optString("records"), new TypeToken<List<InviteRecordEntity>>() {}.getType());

                    if (listDates == null || listDates.size() == 0) {
                        ToastUtils.showToast("没有兑换记录！");
                    }
                    //adapter
                    inviteRecordAdapter = new InviteRecordAdapter(mContext,listDates);
                    //bind
                    lv_inviterecord.setAdapter(inviteRecordAdapter);

                    rl_inviterecord_nochargedhead.setVisibility(View.GONE);
                    noChargedFlag = false;


                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });

    }



}
